####################################################################
## author:    Robert Huber
## file name: greenNationalism_master.R
## Context:   Green Nationalism Project
## started:   2018-12-20
## Summary:   runs R-Scripts
######################################################################

## empty memory (!)
rm(list=ls())

#load packages that are too complicated to feed out of package
#library(ggplot2)
library(tidyverse)

df <- readRDS("./data_clean.rds")
df_5 <- readRDS("./data_clean_treatment5.rds")

#. Figure: Treatment effects (H1) for first three DVs####

df_h1 <- expand.grid(treatment = as.character(c("Helps both (T1)", "Hurts both (T2)")),
                     dv = c("limittrade", "tradeties_personal", "tradeties_country"))

df_h1_loop <- expand.grid(dv = c("limittrade", "tradeties_personal", "tradeties_country"))


AME <- matrix(NA, nrow = 2, ncol = 3)
SE <- matrix(NA, nrow = 2, ncol = 3)
VCOV <- NA

for (i in 1:nrow(df_h1_loop)){
  m <- lm(paste0(df_h1_loop$dv[i], "~", "factor(treatment)", "+ factor(country) + age"), df, subset = treatment %in% c("Control", "Helps both (T1)", "Hurts both (T2)"))
  
  AME[1:2, i] <- coef(m)[2:3]
  SE[1:2, i] <- c(sqrt(vcov(m)[2,2]), sqrt(vcov(m)[3,3]))
  VCOV[i] <- vcov(m)[2,3]
}

df_h1$coef <- c(AME)
df_h1$se <- c(SE)

df_h1[7:9,"dv"] <- c("limittrade", "tradeties_personal", "tradeties_country")

df_h1[7,"coef"] <- abs(df_h1[1,]$coef) - abs(df_h1[2,]$coef)
df_h1[8,"coef"] <- abs(df_h1[3,]$coef) - abs(df_h1[4,]$coef)
df_h1[9,"coef"] <- abs(df_h1[5,]$coef) - abs(df_h1[6,]$coef)

df_h1[7,"se"] <- sqrt(df_h1[1,]$se^2 + df_h1[2,]$se^2 + 2*VCOV[1])
df_h1[8,"se"] <- sqrt(df_h1[3,]$se^2 + df_h1[4,]$se^2 + 2*VCOV[2])
df_h1[9,"se"] <- sqrt(df_h1[5,]$se^2 + df_h1[6,]$se^2 + 2*VCOV[3])


levels <- levels(df_h1$treatment)
levels[length(levels) + 1] <- "Difference in effect\nsize: T1 & T2"
df_h1$treatment <- factor(df_h1$treatment, levels = levels)
df_h1$treatment[is.na(df_h1$treatment)] <- "Difference in effect\nsize: T1 & T2"

df_h1$dv <- factor(df_h1$dv,
                   levels = c("tradeties_personal", "tradeties_country", "limittrade"),
                   labels = c("Trade perception: Egotropic" , "Trade perception: Sociotropic", "Trade restrictions: Environment"))

df_h1$dv <- forcats::fct_rev(df_h1$dv)

df_h1$treatment <- forcats::fct_rev(df_h1$treatment)

ggplot(df_h1, aes(y=coef, x=treatment, colour = dv)) +
  geom_hline(yintercept = 0, colour = "grey", size = 1.25) +
  geom_point(aes(colour = dv, shape = dv), position = position_dodge(width = .5), size = 2) +
  geom_linerange(aes(ymin = coef - 1.96*se, ymax = coef + 1.96*se, colour = dv), position = position_dodge(width = .5), size = 1.1) +
  guides(col = guide_legend(nrow = 3))+
  theme(legend.position="bottom") +
  coord_flip(ylim = c(-.4,.4)) +
  labs(y="\nCoefficient", x="Treatment") +
  theme(text = element_text(size = 18)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  scale_color_discrete(name = "Dependent\nVariable:", breaks = levels(forcats::fct_rev(df_h1$dv))) +
  scale_shape_discrete(name = "Dependent\nVariable:", breaks = levels(forcats::fct_rev(df_h1$dv))) +
  scale_y_continuous(breaks = seq(from = -0.4, to = 0.4, length.out = 9))

ggsave("./output/Figure2.pdf",
       width = 22, height = 15, units = "cm")

####. Regression table for all DVs####

mod1 <- lm(tradeties_personal ~ treatment + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/Table3.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: Environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia"),
               float.pos = "htb",
               caption = "\\textbf{Experimental result}",
               label = "tab:experimentalresults_full", use.packages = F)

mod4 <- nnet::multinom(generaltrade ~ treatment + age + factor(country), df)

texreg::texreg(list(mod4),
               file = "./output/Table2.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia"),
               float.pos = "htb",
               caption = "\\textbf{Experimental results -- Trade restrictions: Economic}",
               label = "tab:experimentalresultsH1_multinom_full", use.packages = F)

#. by country Effects ####

by_Country_ego <- df %>%
  group_by(country) %>%
  do(broom::tidy(lm(tradeties_personal ~ treatment + age, 
                    data = .))) %>%
  ungroup %>% 
  dplyr::rename(model = country) %>%
  mutate(dv = "ego")

by_Country_socio <- df %>%
  group_by(country) %>%
  do(broom::tidy(lm(tradeties_country ~ treatment + age, 
                    data = .))) %>%
  ungroup %>% 
  dplyr::rename(model = country) %>%
  mutate(dv = "socio")

by_Country_limit <- df %>%
  group_by(country) %>%
  do(broom::tidy(lm(limittrade ~ treatment + age, 
                    data = .))) %>%
  ungroup %>% 
  dplyr::rename(model = country) %>%
  mutate(dv = "limit")

by_Country_general <- df %>%
  group_by(country) %>%
  do(broom::tidy(nnet::multinom(generaltrade ~ treatment + age,
                                data = .))) %>%
  ungroup %>% 
  dplyr::rename(model = country) %>%
  rename('dv' = y.level)

df_by_country <- bind_rows(by_Country_ego, by_Country_socio, by_Country_limit, by_Country_general) %>%
  filter(!grepl(x = term, pattern = "T5"))

df_by_country %>%
  filter(dv %in% c("ego", "socio", "limit") & grepl(pattern = "T", term)) %>%
  mutate(term = gsub(pattern = "treatment", replacement = "", x = term)) %>%
  mutate(term = factor(term,
                       levels = c("Hurts home (T4)", "Hurts abroad (T3)",
                                  "Hurts both (T2)", "Helps both (T1)")),
         dv = factor(dv, levels = c("ego", "socio", "limit"),
                     labels = c("Trade Perception:\nEgotropic",
                                "Trade Perception:\nSociotropic",
                                "Trade Restriction:\nEnvironment"))) %>%
  ggplot(., aes(x=term, y= estimate, colour = model, shape = model)) +
  geom_hline(yintercept = 0, colour = "grey", size = 1.25) +
  geom_pointrange(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), position = position_dodge(width = .75)) +
  theme(legend.position="bottom") +
  coord_flip(ylim = c(-.75,.75)) +
  scale_color_discrete(name="Country:") +
  scale_shape_discrete(name="Country:") +
  labs(x="Treatment", y="\nCoefficient") +
  theme(text = element_text(size = 18)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  facet_grid(dv~.) +
  NULL

ggsave("./output/Figure4.pdf",
       width = 22,
       height = 28, units = "cm")


'%!in%' <- function(x,y)!('%in%'(x,y))

df_by_country %>%
  filter(dv %!in% c("ego", "socio", "limit") & grepl(pattern = "T", term)) %>%
  mutate(term = gsub(pattern = "treatment", replacement = "", x = term)) %>%
  mutate(term = factor(term,
                       levels = c("Hurts home (T4)", "Hurts abroad (T3)",
                                  "Hurts both (T2)", "Helps both (T1)")),
         dv = forcats::fct_rev(dv)) %>%
  ggplot(., aes(x=term, y= estimate, colour = model, shape = model)) +
  geom_hline(yintercept = 1, colour = "grey", size = 1.25) +
  geom_pointrange(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), position = position_dodge(width = .75)) +
  theme(legend.position="bottom") +
  coord_flip(ylim = c(-1,3)) +
  scale_color_discrete(name="Country:") +
  scale_shape_discrete(name="Country:") +
  labs(x="Treatment", y="\nCoefficient") +
  theme(text = element_text(size = 18)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  facet_grid(dv~.) +
  NULL

ggsave("./output/FigureA1.pdf",
       width = 22,
       height = 28, units = "cm")

#. Heterogeneous Treatment Effects ####

#.. Nationalism ####

mod1 <- lm(tradeties_personal ~ treatment*nationinterest2 + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment*nationinterest2 + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment*nationinterest2 + age + factor(country), df)

mod4 <- nnet::multinom(generaltrade ~ treatment*nationinterest2 + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA13.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Nationalism",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Nationalism", "T2 x Nationalism", "T3 x Nationalism", "T4 x Nationalism"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Nationalism}",
               label = c("tab:treatmentXNationalism"),
               float.pos = "htb", use.packages = F)

texreg::texreg(list(mod4),
               file = "./output/TableA14.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               float.pos = "htb",
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Nationalism",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Nationalism", "T2 x Nationalism", "T3 x Nationalism", "T4 x Nationalism"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Nationalism}",
               label = c("tab:treatmentXNationalism_multinom"),
               use.packages = F)

#.. Gender ####

mod1 <- lm(tradeties_personal ~ treatment*gender + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment*gender + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment*gender + age + factor(country), df)

mod4 <- nnet::multinom(generaltrade ~ treatment*gender + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA17.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Gender",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Gender", "T2 x Gender", "T3 x Gender", "T4 x Gender"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Gender}",
               label = c("tab:treatmentXGender"),
               float.pos = "htb", use.packages = F)

texreg::texreg(list(mod4),
               file = "./output/TableA18.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               float.pos = "htb",
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Gender",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Gender", "T2 x Gender", "T3 x Gender", "T4 x Gender"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Gender}",
               label = c("tab:treatmentXGender_multinom"),
               use.packages = F)


#.. Environmental Concern ####

mod1 <- lm(tradeties_personal ~ treatment*envCon + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment*envCon + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment*envCon + age + factor(country), df)

mod4 <- nnet::multinom(generaltrade ~ treatment*envCon + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA15.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Environmental Concern",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Environmental Concern", "T2 x Environmental Concern", "T3 x Environmental Concern", "T4 x Environmental Concern"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Environmental Concern}",
               label = c("tab:treatmentXEnvironmentalConcern"),
               float.pos = "htb", use.packages = F)

texreg::texreg(list(mod4),
               file = "./output/TableA16.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               float.pos = "htb",
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Environmental Concern",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Environmental Concern", "T2 x Environmental Concern", "T3 x Environmental Concern", "T4 x Environmental Concern"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Environmental Concern}",
               label = c("tab:treatmentXEnvironmentalConcern_multinom"),
               use.packages = F)

#.. Left-Right ####

mod1 <- lm(tradeties_personal ~ treatment*lr + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment*lr + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment*lr + age + factor(country), df)

mod4 <- nnet::multinom(generaltrade ~ treatment*lr + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA19.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Left-Right",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Left-Right", "T2 x Left-Right", "T3 x Left-Right", "T4 x Left-Right"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Left-Right}",
               label = c("tab:treatmentXLeftRight"),
               float.pos = "htb", use.packages = F)

texreg::texreg(list(mod4),
               file = "./output/TableA20.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               float.pos = "htb",
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Left-Right",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Left-Right", "T2 x Left-Right", "T3 x Left-Right", "T4 x Left-Right"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Left-Right}",
               label = c("tab:treatmentXLeftRight_multinom"),
               use.packages = F)

# .. Income ####

mod1 <- lm(tradeties_personal ~ treatment*income + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment*income + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment*income + age + factor(country), df)

mod4 <- nnet::multinom(generaltrade ~ treatment*income + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA21.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Income",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Income", "T2 x Income", "T3 x Income", "T4 x Income"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Income}",
               label = c("tab:treatmentXIncome"),
               float.pos = "htb", use.packages = F)

texreg::texreg(list(mod4),
               file = "./output/TableA22.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               float.pos = "htb",
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Income",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Income", "T2 x Income", "T3 x Income", "T4 x Income"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Income}",
               label = c("tab:treatmentXIncome_multinom"),
               use.packages = F)

#.. Education ####

mod1 <- lm(tradeties_personal ~ treatment*education + age + factor(country), df)

mod2 <- lm(tradeties_country ~ treatment*education + age + factor(country), df)

mod3 <- lm(limittrade ~ treatment*education + age + factor(country), df)

mod4 <- nnet::multinom(generaltrade ~ treatment*education + age + factor(country), df)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA23.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               stars = c(.1, .05, .01),
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Education",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Education", "T2 x Education", "T3 x Education", "T4 x Education"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Education}",
               label = c("tab:treatmentXEducation"),
               float.pos = "htb", use.packages = F)


texreg::texreg(list(mod4),
               file = "./output/TableA24.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               float.pos = "htb",
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Education",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia",
                                     "T1 x Education", "T2 x Education", "T3 x Education", "T4 x Education"),
               caption = "\\textbf{Heterogeneous Treatment Effects -- Education}",
               label = c("tab:treatmentXEducation_multinom"),
               use.packages = F)

summary(margins::margins(mod1,
                         var = "treatment",
                         at = list(education = c("No highschool", "Highschool")))) %>%
  mutate(factor = factor(gsub("treatment", "", factor),
                         levels = c("Hurts home (T4)",
                                    "Hurts abroad (T3)",
                                    "Hurts both (T2)",
                                    "Helps both (T1)")),
         education = factor(education, 
                            levels = c("No highschool", "Highschool")),
         dv = "Trade perceptions\n(Egotropic)") %>%
  bind_rows(., summary(margins::margins(mod2,
                                        var = "treatment",
                                        at = list(education = c("No highschool", "Highschool")))) %>%
              mutate(factor = factor(gsub("treatment", "", factor),
                                     levels = c("Hurts home (T4)",
                                                "Hurts abroad (T3)",
                                                "Hurts both (T2)",
                                                "Helps both (T1)")),
                     education = factor(education, 
                                        levels = c("No highschool", "Highschool")),
                     dv = "Trade perceptions\n(Sociotropic)")) %>%
  bind_rows(summary(margins::margins(mod3,
                                     var = "treatment",
                                     at = list(education = c("No highschool", "Highschool")))) %>%
              mutate(factor = factor(gsub("treatment", "", factor),
                                     levels = c("Hurts home (T4)",
                                                "Hurts abroad (T3)",
                                                "Hurts both (T2)",
                                                "Helps both (T1)")),
                     education = factor(education, 
                                        levels = c("No highschool", "Highschool")),
                     dv = "Trade restriction\n(Environment)")) %>%
  ggplot(., aes(x=factor, y =AME, colour = education, shape = education)) +
  geom_hline(yintercept = 0, colour = "grey", size = 1.25) +
  geom_linerange(aes(ymin = AME - 1.645 * SE, ymax = AME + 1.645 * SE), size = 1.1, position = position_dodge(width = .25)) +
  geom_pointrange(aes(ymin = lower, ymax = upper), position = position_dodge(width = .25)) +
  facet_grid(dv~.) + 
  labs(x = "Treatment Condition", y = "Average Marginal Effect") +
  scale_colour_discrete("Education:") +
  scale_shape_discrete("Education:") +
  theme(legend.position = "bottom") +
  theme(text = element_text(size = 18)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  coord_flip() + 
  NULL

ggsave("./output/Figure3.pdf",
       width = 22, height = 18, units = "cm")


#. Treatment 5 ####

df_5$treatment <- factor(ifelse(df_5$treatment == "Hurts abroad extensively (T5)", "Hurts abroad (T3)",
                                as.character(df_5$treatment)),
                         levels = c("Control", "Helps both (T1)", "Hurts both (T2)",
                                    "Hurts abroad (T3)", "Hurts home (T4)"))


mod1 <- lm(tradeties_personal ~ treatment + age + factor(country), df_5)

mod2 <- lm(tradeties_country ~ treatment + age + factor(country), df_5)

mod3 <- lm(limittrade ~ treatment + age + factor(country), df_5)

texreg::texreg(list(mod1, mod2, mod3),
               file = "./output/TableA25.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Trade perception: Egotropic", "Trade perception: Sociotropic", "Trade restrictions: Environment"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia"),
               float.pos = "htb",
               caption = "\\textbf{Experimental result: Treatment 3 and 5 combined}",
               label = "tab:treatment5", use.packages = F)

mod4 <- nnet::multinom(generaltrade ~ treatment + age + factor(country), df_5)

texreg::texreg(list(mod4),
               file = "./output/TableA26.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               omit.coef = c("T5"),
               custom.model.names = c("Oppose", "Have not thought", "Don't know"),
               custom.coef.names = c("Intercept", "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)",
                                     "Age",
                                     "Germany", "USA", "Canada", "Switzerland", "Australia"),
               float.pos = "htb",
               caption = "\\textbf{Experimental result: Treatment 3 and 5 combined}",
               label = "tab:treatment5_multinom", use.packages = F)


# Manipulation Check ####

se <- function(x) sqrt(var(x, na.rm = T)/length(x))

df_mani <- df %>% 
  group_by(treatment) %>%
  summarise(mean = mean(manipcheck_tradeenveffects, na.rm=T), n = n(), se = se(manipcheck_tradeenveffects), lower = mean - 1.96*se, upper = mean + 1.96*se)

lower_ctr <- df_mani[1,]$lower
upper_ctr <- df_mani[1,]$upper

ggplot(df_mani, aes(x=factor(treatment), y=mean)) + 
  geom_bar(stat = "identity", fill = "white", colour = "black") +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = .5) +
  ylab("Mean") +
  scale_x_discrete(name = "\nTreatment") +
  theme(text = element_text(size = 18)) +
  coord_cartesian(ylim = c(3, 4.5)) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1))

ggsave("./output/Figure1.pdf",
       width = 22, height = 15, units = "cm")

mod1 = lm(manipcheck_tradeenveffects ~ treatment + factor(country), df)

texreg::texreg(list(mod1),
               file = "./output/TableA10.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Baseline"),
               custom.coef.names = c(NA, "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)", 
                                     "Germany", "USA", "Canada", "Switzerland", "Australia"),#,
               float.pos = "htb",
               caption = "\\textbf{Manipulation Check}",
               label = "tab:manipulation")

# Balance Tests ####

m_age <- lm(age ~ treatment, df)
m_edu <- glm(education == 'Highschool' ~ treatment, df, family = "binomial")
m_gender <- glm(gender == 'Female' ~ treatment, df, family = "binomial")
m_income <- lm(income ~ treatment, df)
m_national <- lm(nationinterest ~ treatment, df)
m_envCon <- glm(envCon == 'Concerned' ~ treatment, df, family = "binomial")
m_cliSkep <- glm(climSkep == 'Believer' ~ treatment, df, family = "binomial")
m_employ <- glm(employment == 'Employed' ~ treatment, df, family = "binomial")
m_lr <- lm(lr ~ treatment, df)
m_financialSit <- lm(financialSit ~ treatment, df)
m_trade_know <- lm(trade_know ~ treatment, df)

texreg::texreg(list(m_age, m_gender, m_lr),
               file = "./output/TableA7.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Age", "Gender", "Political Ideology"),
               custom.coef.names = c(NA, "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)"),
               float.pos = "htb",
               caption = "\\textbf{Balance table: Age, Gender, Political Ideology}",
               label = "tab:balance_demo")

texreg::texreg(list(m_income, m_employ, m_edu, m_financialSit),
               file = "./output/TableA8.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Income", "Employment", "Education", "Financial Situation"),
               custom.coef.names = c(NA, "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)"),
               float.pos = "htb",
               caption = "\\textbf{Balance table: Income, Employment, Education and Financial Situation}",
               label = "tab:balance_econ")

texreg::texreg(list(m_envCon, m_cliSkep, m_trade_know, m_national),
               file = "./output/TableA9.tex",
               leading.zero = T,
               single.row = F,
               digits = 2,
               custom.model.names = c("Environmental Concern", "Climate Scepticism", "Trade Knowledge", "Nationalism"),
               custom.coef.names = c(NA, "Helps both (T1)", "Hurts both (T2)", "Hurts abroad (T3)", "Hurts home (T4)"),
               float.pos = "htb",
               caption = "\\textbf{Balance table: Environmental Concern, Climate Scepticism, Trade Knowledge, Nationalism}",
               label = "tab:balance_oth")
